package com.fingard.dsp.bank.directbank.cmb01;

import com.fingard.dsp.bank.directbank.bankInterface.IQuotaInfo;
import com.fingard.dsp.bank.reqrespobj.qryquotainfo.ReqQryQuotaInfo;
import com.fingard.dsp.bank.reqrespobj.qryquotainfo.RetAtsQuotaInfo;
import com.fingard.dsp.bank.reqrespobj.qryquotainfo.RetAtsQuotaInfoDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/11/15.
 */
public class CMB01QryQuotaInfo extends CMB01Base implements IQuotaInfo {
    @Override
    public RetAtsQuotaInfo queryQuotaInfo(ReqQryQuotaInfo reqQryQuotaInfo) throws Exception {
        RetAtsQuotaInfo retAts = new RetAtsQuotaInfo(reqHeader);
        retAts.writeStart();
        String tmpRespCode ="";
        String tmpRespInfo ="";
        try {
            CMB01Header header =  new CMB01Header();
            header.FUNNAM = "TDCRTQUY";
            XmlTextWriter xmlWriter =  new XmlTextWriter();
            writeCommonHeader(xmlWriter,header);
//            输入 TDCRTQUYX 输入属性 单记录
            xmlWriter.writeStartElement("TDCRTQUYX");
//            直连对接企业系统客户号  COREID C(10) 否
            xmlWriter.writeElementString("COREID",bankFront.org);
//            对接企业类型 COTYPE C(1) 否
            xmlWriter.writeElementString("COTYPE", "1");
//            核心企业组织机构代码 SCOCOD C(10) 可
            xmlWriter.writeElementString("SCOCOD", "");
//            客户组织机构代码 CERTID C(10) 可
            xmlWriter.writeElementString("CERTID","" );
//            企业外部编码 TPCODE C(40) 可
            xmlWriter.writeElementString("TPCODE", reqQryQuotaInfo.DealerCode);
//            授信额度编号 CRDTID C(15) 可
            xmlWriter.writeElementString("CRDTID", "");
//            额度状态 CRESTS C(4) 可
            xmlWriter.writeElementString("CRESTS", "APPR");
//            产品类型 PROTPE C(2) 否
            xmlWriter.writeElementString("PROTPE", "02");
//            保留字段 REV50L C(50) 可
            xmlWriter.writeElementString("REV50L","" );
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                if (tmpBkReader != null) {
                    String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                    if("0".equals(bankRetCode)) {
                        tmpRespCode ="0";
                        tmpRespInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                        while (tmpBkReader.read("/CMBSDKPGK/TDCRTQUYZ")){
                            RetAtsQuotaInfoDetail detail = new RetAtsQuotaInfoDetail();
//                            客户组织机构代码 CERTID C(10) 可
                            detail.DealerCode = tmpBkReader.readedXElement.getSingleText("CERTID");
//                            额度起始日 STADAT D 否
                            detail.LimitBeginDate = tmpBkReader.readedXElement.getSingleText("STADAT");
//                            额度到期日 ENDDAT D 否
                            detail.LimitEndDate = tmpBkReader.readedXElement.getSingleText("ENDDAT");
                            if("0".equals(tmpBkReader.readedXElement.getSingleText("ENDDAT"))) {
//                            名义金额 CRDAMT M 可
                                detail.Limit = tmpBkReader.readedXElement.getSingleText("CRDAMT");
//                            已用名义额度 CUDAMT M 可
                                detail.UsedLimit = tmpBkReader.readedXElement.getSingleText("CUDAMT");
//                            可用名义额度 CAVAMT M 可
                                detail.UnuseLimit = tmpBkReader.readedXElement.getSingleText("CAVAMT");
                            }else{
//                                敞口金额 EXOAMT M
                                detail.Limit = tmpBkReader.readedXElement.getSingleText("EXOAMT");
//                                已用敞口额度 EUDAMT M
                                detail.UsedLimit = tmpBkReader.readedXElement.getSingleText("EUDAMT");
//                                可用敞口额度 EAVAMT M
                                detail.UnuseLimit = tmpBkReader.readedXElement.getSingleText("EAVAMT");
                            }
                            retAts.writeRetDetail(detail);
                        }
                    }else{
                        String bankRetMsg =tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                        throw new Exception(bankRetCode+":"+bankRetMsg);
                    }
                }else {
                    throw new Exception(tmpBkReader.toString());
                }
            } else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            tmpRespCode ="-1";
            tmpRespInfo = ex.getMessage();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
